package com.example.za.account.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;

import com.example.za.account.R;
import com.example.za.account.model.User;
import com.example.za.account.utils.Constant;
import com.example.za.account.utils.StringUtils;
import com.example.za.account.utils.T;
import com.example.za.account.view.NormalEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class ForgetPassActivity extends BaseActivity {

    @BindView(R.id.forget_email)
    NormalEditText forgetEmail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass);
        ButterKnife.bind(this);
        setTitle(getString(R.string.find_password));
        showBackwardView(true);
    }

    @Override
    protected Activity getSubActivity() {
        return this;
    }

    @OnClick(R.id.btn_pass_next)
    public void onViewClicked() {
        final String email = forgetEmail.getText().toString().trim();
        if (!TextUtils.isEmpty(email)) {
            if (StringUtils.checkEmail(email)) {
                BmobQuery<User> query = new BmobQuery<>();
                query.addWhereEqualTo("email", email);
                query.findObjects(new FindListener<User>() {
                    @Override
                    public void done(List<User> list, BmobException e) {
                        if (e != null) {
                            T.showShort(getApplicationContext(), getString(R.string.check_net_connect));
                        } else {
                            if (list != null && list.size() > 0) {
                                BmobUser.resetPasswordByEmail(email, new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e != null) {
                                            T.showShort(getApplicationContext(), getString(R.string.reset_pass_fail));
                                        } else {
                                            Intent intent = new Intent(ForgetPassActivity.this, ForgetNextActivity.class);
                                            intent.putExtra(Constant.FORGET_PASS, email);
                                            startActivity(intent);
                                        }
                                    }
                                });
                            } else {
                                T.showShort(getApplicationContext(), getString(R.string.unregister_email));
                            }
                        }
                    }
                });
            } else {
                T.showShort(this, getString(R.string.input_right_email));
            }
        } else {
            T.showShort(this, getString(R.string.input_email));
        }
    }
}
